package yygh.hosp.Service.imp;

import com.alibaba.fastjson.JSONObject;
import yygh.hospital.model.hosp.Hospital;
import yygh.hospital.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import yygh.cmn.client.DictFeignClient;
import yygh.hosp.Repository.HospitalRepository;
import yygh.hosp.Service.HospitalService;

import java.util.*;

@Service
public class HosipitalServiceImp implements HospitalService {

    @Autowired
    HospitalRepository hospitalRepository;


    @Autowired
    DictFeignClient dictFeignClient;
    /**
     *    医院信息接口的Service层
     * @param map
     */
    @Override
    public void save(Map<String, Object> map) {
        //首先将传过来的map集合转化成字符串
        String string = JSONObject.toJSONString(map);
        //然后利用json工具类将字符串转换成Hospital对象
        Hospital hospital = JSONObject.parseObject(string,Hospital.class);
        //根据hoscode查找是否存在该对象信息
        String hoscode = hospital.getHoscode();
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospitalByHoscode!=null){
            //如果mongodb中存在该对象，则进行修改
            hospital.setStatus(hospitalByHoscode.getStatus());
            hospital.setCreateTime(hospitalByHoscode.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {
            //如果不存在该对象，则进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }
    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }
    /**
     *  管理平台医院列表展示
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    //医院列表的展示
    @Override
    public Page<Hospital> getPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable= PageRequest.of(page-1,limit);
        //利用mongodb进行条件模糊查询
        ExampleMatcher matcher=ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example<Hospital> hospitalExample = Example.of(hospital,matcher);
        Page<Hospital> hospitalPage = hospitalRepository.findAll(hospitalExample, pageable);
        //调用微服务service_cmn相应接口，利用流的形式遍历所有的医院分页信息，实现数据字典信息的注入（医院等级、省市区地址）
        hospitalPage.getContent().stream().forEach(item ->{
            this.setHospitalHostType(item);
        });
        return  hospitalPage;
    }





    //对每一个获取到的医院信息实现数据字典信息（医院等级、省市区地址）的注入
    private Hospital setHospitalHostType(Hospital hospital) {
        //实现医院等级的注入
        String hostypeString = dictFeignClient.getName(hospital.getHostype(), "Hostype");
        //将医院等级封装到hospital的其他属性paraMap中
        hospital.getParam().put("hostypeString",hostypeString);
        //实现省市区的注入
        String provinceCode = dictFeignClient.getName(hospital.getProvinceCode());
        String cityCode = dictFeignClient.getName(hospital.getCityCode());
        String districtCode = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("fullAddress",provinceCode+cityCode+districtCode);
        return hospital;
    }

    // 更新医院的状态信息
    @Override
    public void updateHospStatus(String id, int status) {
        //根据id值查询到对应的医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        //设置医院的上线状态
        hospital.setStatus(1);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    //封装医院的详细信息
    @Override
    public Map<Object, Object> getHospById(String id) {
        //首先根据传入的id值获取指定的医院对象
        Hospital hospital = hospitalRepository.findById(id).get();
        //首先获取省市地区
        this.setHospitalHostType(hospital);
        //创建一个map集合，来保存医院对象，以及相应的预约信息
        Map<Object,Object> map=new HashMap<>();
        map.put("hospital",hospital);
        //封装医院的预约规则
        map.put("bookingRule",hospital.getBookingRule());
        return  map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital!=null){
            return hospital.getHosname();
        }
        return  null;
    }

    /**
     *  前台用户系统的相关Service层实现代码
     * @param hosname
     * @return
     */
    //实现根据医院名称关键字搜索医院列表
    @Override
    public List<Hospital> getByHospName(String hosname) {
        List <Hospital> hospitalList=hospitalRepository.getHospitalByHosnameLike(hosname);
        return  hospitalList;
    }

    //Service层，根据医院编号获取详细的预约挂号信息
    @Override
    public Map<String, Object> item(String hoscode) {
        //获取医院的信息
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);

        //创建一个map集合，来保存医院对象，以及相应的预约信息
        Map<String,Object> map=new HashMap<>();
        map.put("hospital",hospital);
        //封装医院的预约规则
        map.put("bookingRule",hospital.getBookingRule());

        return  map;

    }

}
